#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <windows.h>

// 浪漫文案数组
const char* MESSAGES[] = {
    "一生浪漫的开始",
    "与你相遇的瞬间",
    "心动就在此刻",
    "浪漫的爱心只为你",
    "关于爱的浪漫代码",
    "属于我们的浪漫",
    "这个冬天，把爱给你"
};
#define MSG_COUNT 7

// 窗口和粒子参数
#define SCREEN_WIDTH 80
#define SCREEN_HEIGHT 40
#define NUM_POINTS 100  // 爱心点数

// 清屏函数
void clearScreen() {
    system("cls");
}

// 生成爱心的基础点
void generate_heart_points(float points[NUM_POINTS][2]) {
    for (int i = 0; i < NUM_POINTS; i++) {
        float t = i * 3.14159f / 50;
        points[i][0] = 16 * pow(sin(t), 3);
        points[i][1] = 13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t);
    }
}

// 缩放并定位爱心点到控制台坐标
void scale_and_position(float points[NUM_POINTS][2], int scaled[NUM_POINTS][2], float scale, int offset_x, int offset_y) {
    for (int i = 0; i < NUM_POINTS; i++) {
        scaled[i][0] = offset_x + (int)(points[i][0] * scale);
        scaled[i][1] = offset_y + (int)(points[i][1] * scale);
    }
}

// 设置控制台文字颜色（粉色）
void setPinkColor() {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
}

// 重置控制台文字颜色
void resetColor() {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}

int main() {
    srand((unsigned int)time(NULL));
    float heart_points[NUM_POINTS][2];
    generate_heart_points(heart_points);

    while (1) {
        clearScreen();
        float scale = (float)rand() / RAND_MAX * 2 + 2;
        int offset_x = SCREEN_WIDTH / 2;
        int offset_y = SCREEN_HEIGHT / 2;
        int scaled_points[NUM_POINTS][2];
        scale_and_position(heart_points, scaled_points, scale, offset_x, offset_y);

        const char* msg = MESSAGES[rand() % MSG_COUNT];

        // 绘制爱心粒子（用*号表示）
        for (int i = 0; i < NUM_POINTS; i++) {
            int x = scaled_points[i][0];
            int y = scaled_points[i][1];
            if (x >= 0 && x < SCREEN_WIDTH && y >= 0 && y < SCREEN_HEIGHT) {
                COORD coord;
                coord.X = x;
                coord.Y = y;
                HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
                SetConsoleCursorPosition(hConsole, coord);
                setPinkColor();
                printf("*");
                resetColor();
            }
        }

        // 绘制文案
        int msg_len = 0;
        while (msg[msg_len] != '\0') msg_len++;
        COORD coord;
        coord.X = offset_x - msg_len / 2;
        coord.Y = offset_y;
        HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        SetConsoleCursorPosition(hConsole, coord);
        setPinkColor();
        printf("%s", msg);
        resetColor();

        Sleep(1500);
    }

    return 0;
}